package com.pageoffice;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

/**
 * Servlet implementation class UploadServlet
 */
@WebServlet("/upload")
@MultipartConfig
public class UploadServlet extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = -9119249423271675367L;

	
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 1、 获取文件的存储路径
		String realPath = request.getServletContext().getRealPath(File.separator + "userfiles");
		File file = new File(realPath);
		if (!file.exists()) {
			file.mkdirs();
			System.out.println("路径： " + realPath + " 创建成功!");
		}
		System.out.println(request.getParameter("username"));
		Collection<Part> parts = request.getParts();
		System.out.println(parts.size());
		
		// 2、获取上传文件对象的集合
			// 单个文件上传
			Part part = request.getPart("uploadfile");
			// 获取header 如： form-data; name="uploadfile"; filename="2018-08-11
			// 开【华夏视讯网首发hxsxw.com】.mkv"
			String header = part.getHeader("content-disposition");
			// 获取文件名
			String fileName = header.split(";")[2].split("=")[1].replaceAll("\"", "");
			// 执行写入操作 --- 上传到指定的目录
			part.write(realPath + File.separator + fileName);

		request.setAttribute("message", "文件上传成功!");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/json");
		PrintWriter out = response.getWriter();
	//	out.println(true);
		out.println("{\"msg\":\"文件上传成功!\"}");
		out.flush();
		out.close();
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

	/**
	 * 根据请求头解析出文件名 请求头的格式：火狐和google浏览器下：form-data; name="file";
	 * filename="snmp4j--api.zip" IE浏览器下：form-data; name="file";
	 * filename="E:\snmp4j--api.zip"
	 * 
	 * @param header
	 *            请求头
	 * @return 文件名
	 * @see https://www.cnblogs.com/xdp-gacl/p/4224960.html
	 * @date 2018年8月12日 22:57:27
	 */
	public String getFileName(String header) {
		/**
		 * String[] tempArr1 = header.split(";");代码执行完之后，在不同的浏览器下，tempArr1数
		 * 组里面的内容稍有区别
		 * 火狐或者google浏览器下：tempArr1={form-data,name="file",filename="snmp4j--api.zip"}
		 * IE浏览器下：tempArr1={form-data,name="file",filename="E:\snmp4j--api.zip"}
		 */
		String[] tempArr1 = header.split(";");
		/**
		 * 火狐或者google浏览器下：tempArr2={filename,"snmp4j--api.zip"}
		 * IE浏览器下：tempArr2={filename,"E:\snmp4j--api.zip"}
		 */
		String[] tempArr2 = tempArr1[2].split("=");
		// 获取文件名，兼容各种浏览器的写法
		String fileName = tempArr2[1].substring(tempArr2[1].lastIndexOf("\\") + 1).replaceAll("\"", "");
		return fileName;
	}

}
